// pages/home/search/index.js
import api from '../../../api/index'
import constant from '../../../utils/constant'
import { tansParams } from '../../../utils/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    heightRange: constant.HEIGHT_RANGE(),
    weightRange: constant.WEIGHT_RANGE(),
    ageRange: constant.AGE_RANGE(),
    wIncomeRange: constant.W_INCOME_RANGE(),
    wEduRange: constant.W_EDU_RANGE(),
    user: {},
    search: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    api.user.getProfile().then(res=>{
      console.log(res)
      if(res.WCompany!=undefined){
        res.WCompany = res.WCompany.split(',')
      }
      if(res.WMarital!=undefined){
        res.WMarital = res.WMarital.split(',')
      }
      let WProvince, WCity
      if(wx.$isNull(res.WProvince)){
        WProvince ='不限' 
      }else{
        WProvince = res.WProvince
      }
      if(wx.$isNull(res.WCity)){
        WCity = '不限'
      }else{
        WCity = res.WCity
      }
      let WHeight = []
      WHeight.push(res.WHeightMin)
      WHeight.push(res.WHeightMax)
      res.WHeight = WHeight

      let sex = '女'
      if(res.sex == '女'){
        sex = '男'
      }

      let wAgeMin = '1950-01-01', wAgeMax = '3000-12-31'
      const date = new Date()
      let y = date.getFullYear()
      if(!wx.$isNull(res.WAgeMin)){
        wAgeMax = y-res.WAgeMin + '-12-31'
      }
      if(!wx.$isNull(res.WAgeMax)){
        wAgeMin = y-res.WAgeMax + '-01-01'
      }

      let search ={
        lifeCity: res.lifeCity,
        sex: sex,
        wProvince: WProvince,
        wCity: WCity,
        wAgeMin: wAgeMin,
        wAgeMax: wAgeMax,
        wHeightMin: res.WHeightMin,
        wHeightMax: res.WHeightMax,
        wEducation: res.WEducation,
        wCompany: res.WCompany.toString(),
        wIncome: res.WIncome,
        wMarital: res.WMarital.toString()
      }
      console.log(search)
      this.setData({
        user: res,
        search: search
      })

    })
  },

  onWPicker(e){
    let inx = e.currentTarget.dataset.index
    let v = e.detail, search = this.data.search
    if(inx == 'wArea'){
      if(!wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        search.wProvince = v.val[0]
        search.wCity = v.val[1]
      }else if(!wx.$isNull(v.val[0]) && wx.$isNull(v.val[1])){
        search.wProvince = v.val[0]
        search.wCity = ''
      }else{
        search.wProvince = ''
        search.wCity = ''
      }
    }else if(inx == 'wAge'){
      if(!wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        search.wAgeMin = v.val[0]
        search.wAgeMax = v.val[1]
      }else if(wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        search.wAgeMin = '',
        search.wAgeMax = v.val[1]
      }else if(!wx.$isNull(v.val[0]) && wx.$isNull(v.val[1])){
        search.wAgeMin = v.val[0]
        search.wAgeMax = ''
      }else{
        search.wAgeMin = ''
        search.wAgeMax = ''
      }
    }else if(inx == 'wheight'){
      if(!wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        search.wHeightMin = v.val[0]
        search.wHeightMax = v.val[1]
      }else if(wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        search.wHeightMin = '',
        search.wHeightMax = v.val[1]
      }else if(!wx.$isNull(v.val[0]) && wx.$isNull(v.val[1])){
        search.wHeightMin = v.val[0]
        search.wHeightMax = ''
      }else{
        search.wHeightMin = ''
        search.wHeightMax = ''
      }
    }else if(inx == 'wEducation'){
      if(wx.$isNull(v.val)){
        search.wEducation = ''
      }else{
        search.wEducation = v.val
      }
    }else if(inx == 'wCompany'){
      if(wx.$isNull(v.val)){
        search.wCompany = ''
      }else{
        search.wCompany = v.val.toString()
      }
    }else if(inx == 'wIncome'){
      if(wx.$isNull(v.val)){
        search.wIncome = ''
      }else{
        search.wIncome = v.val
      }
    }else if(inx == 'wMarital'){
      if(wx.$isNull(v.val)){
        search.wMarital = ''
      }else{
        search.wMarital = v.val.toString()
      }
    }

    this.setData({
      search: search
    })
  },
  onSearch(){
    let list = {
      content: JSON.stringify(this.data.search)
    }

    wx.navigateTo({
      url:'../userList/index?' + tansParams(list),
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },





  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})